home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 733 / termcap / source.lha / tparam.c < prev   
C/C++ Source or Header  |  1992-09-27  |  7KB  |  277 lines

  1. /* Merge parameters into a termcap entry string.
  2.    Copyright (C) 1985, 1987 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* config.h may rename various library functions such as malloc.  */
  19. #ifdef emacs
  20. #include "config.h"
  21. #else
  22. #if defined(USG) || defined(STDC_HEADERS)
  23. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  24. #endif
  25. #endif
  26.  
  27. /* Assuming STRING is the value of a termcap string entry
  28.    containing `%' constructs to expand parameters,
  29.    merge in parameter values and store result in block OUTSTRING points to.
  30.    LEN is the length of OUTSTRING.  If more space is needed,
  31.    a block is allocated with `malloc'.
  32.  
  33.    The value returned is the address of the resulting string.
  34.    This may be OUTSTRING or may be the address of a block got with `malloc'.
  35.    In the latter case, the caller must free the block.
  36.  
  37.    The fourth and following args to tparam serve as the parameter values.  */
  38.  
  39. static char *tparam1 ();
  40.  
  41. /* VARARGS 2 */
  42. char *
  43. tparam (string, outstring, len, arg0, arg1, arg2, arg3)
  44.      char *string;
  45.      char *outstring;
  46.      int len;
  47.      int arg0, arg1, arg2, arg3;
  48. {
  49. #ifdef NO_ARG_ARRAY
  50.   int arg[4];
  51.   arg[0] = arg0;
  52.   arg[1] = arg1;
  53.   arg[2] = arg2;
  54.   arg[3] = arg3;
  55.   return tparam1 (string, outstring, len, 0, 0, arg);
  56. #else
  57.   return tparam1 (string, outstring, len, 0, 0, &arg0);
  58. #endif
  59. }
  60.  
  61. char *BC;
  62. char *UP;
  63.  
  64. static char tgoto_buf[50];
  65.  
  66. char *
  67. tgoto (cm, hpos, vpos)
  68.      char *cm;
  69.      int hpos, vpos;
  70. {
  71.   int args[2];
  72.   if (!cm)
  73.     return 0;
  74.   args[0] = vpos;
  75.   args[1] = hpos;
  76.   return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
  77. }
  78.  
  79. static char *
  80. tparam1 (string, outstring, len, up, left, argp)
  81.      char *string;
  82.      char *outstring;
  83.      int len;
  84.      char *up, *left;
  85.      register int *argp;
  86. {
  87.   register int c;
  88.   register char *p = string;
  89.   register char *op = outstring;
  90.   char *outend;
  91.   int outlen = 0;
  92.  
  93.   register int tem;
  94.   int *oargp = argp;
  95.   int doleft = 0;
  96.   int doup = 0;
  97.  
  98.   outend = outstring + len;
  99.  
  100.   while (1)
  101.     {
  102.       /* If the buffer might be too short, make it bigger.  */
  103.       if (op + 5 >= outend)
  104.     {
  105.       register char *new;
  106.       if (outlen == 0)
  107.         {
  108.           new = (char *) malloc (outlen = 40 + len);
  109.           outend += 40;
  110.           bcopy (outstring, new, op - outstring);
  111.         }
  112.       else
  113.         {
  114.           outend += outlen;
  115.           new = (char *) realloc (outstring, outlen *= 2);
  116.         }
  117.       op += new - outstring;
  118.       outend += new - outstring;
  119.       outstring = new;
  120.     }
  121.       if (!(c = *p++))
  122.     break;
  123.       if (c == '%')
  124.     {
  125.       c = *p++;
  126.       tem = *argp;
  127.       switch (c)
  128.         {
  129.         case 'd':        /* %d means output in decimal */
  130.           if (tem < 10)
  131.         goto onedigit;
  132.           if (tem < 100)
  133.         goto twodigit;
  134.         case '3':        /* %3 means output in decimal, 3 digits. */
  135.           if (tem > 999)
  136.         {
  137.           *op++ = tem / 1000 + '0';
  138.           tem %= 1000;
  139.         }
  140.           *op++ = tem / 100 + '0';
  141.         case '2':        /* %2 means output in decimal, 2 digits. */
  142.         twodigit:
  143.           tem %= 100;
  144.           *op++ = tem / 10 + '0';
  145.         onedigit:
  146.           *op++ = tem % 10 + '0';
  147.           argp++;
  148.           break;
  149.  
  150.         case 'C':
  151.           /* For c-100: print quotient of value by 96, if nonzero,
  152.          then do like %+ */
  153.           if (tem >= 96)
  154.         {
  155.           *op++ = tem / 96;
  156.           tem %= 96;
  157.         }
  158.         case '+':        /* %+x means add character code of char x */
  159.           tem += *p++;
  160.         case '.':        /* %. means output as character */
  161.           if (left)
  162.         {
  163.           /* If want to forbid output of 0 and \n and \t,
  164.              and this is one of them, increment it.  */
  165.           while (tem == 0 || tem == '\n' || tem == '\t')
  166.             {
  167.               tem++;
  168.               if (argp == oargp)
  169.             doup++, outend -= strlen (up);
  170.               else
  171.             doleft++, outend -= strlen (left);
  172.             }
  173.         }
  174.           *op++ = tem | 0200;
  175.         case 'f':        /* %f means discard next arg */
  176.           argp++;
  177.           break;
  178.  
  179.         case 'b':        /* %b means back up one arg (and re-use it) */
  180.           argp--;
  181.           break;
  182.  
  183.         case 'r':        /* %r means interchange following two args */
  184.           argp[0] = argp[1];
  185.           argp[1] = tem;
  186.           oargp++;
  187.           break;
  188.  
  189.         case '>':        /* %>xy means if arg is > char code of x, */
  190.           if (argp[0] > *p++) /* then add char code of y to the arg, */
  191.         argp[0] += *p;    /* and in any case don't output. */
  192.           p++;        /* Leave the arg to be output later. */
  193.           break;
  194.  
  195.         case 'a':        /* %a means arithmetic */
  196.           /* Next character says what operation.
  197.          Add or subtract either a constant or some other arg */
  198.           /* First following character is + to add or - to subtract
  199.          or = to assign.  */
  200.           /* Next following char is 'p' and an arg spec
  201.          (0100 plus position of that arg relative to this one)
  202.          or 'c' and a constant stored in a character */
  203.           tem = p[2] & 0177;
  204.           if (p[1] == 'p')
  205.         tem = argp[tem - 0100];
  206.           if (p[0] == '-')
  207.         argp[0] -= tem;
  208.           else if (p[0] == '+')
  209.         argp[0] += tem;
  210.           else if (p[0] == '*')
  211.         argp[0] *= tem;
  212.           else if (p[0] == '/')
  213.         argp[0] /= tem;
  214.           else
  215.         argp[0] = tem;
  216.  
  217.           p += 3;
  218.           break;
  219.  
  220.         case 'i':        /* %i means add one to arg, */
  221.           argp[0] ++;    /* and leave it to be output later. */
  222.           argp[1] ++;    /* Increment the following arg, too!  */
  223.           break;
  224.  
  225.         case '%':        /* %% means output %; no arg. */
  226.           goto ordinary;
  227.  
  228.         case 'n':        /* %n means xor each of next two args with 140 */
  229.           argp[0] ^= 0140;
  230.           argp[1] ^= 0140;
  231.           break;
  232.  
  233.         case 'm':        /* %m means xor each of next two args with 177 */
  234.           argp[0] ^= 0177;
  235.           argp[1] ^= 0177;
  236.           break;
  237.  
  238.         case 'B':        /* %B means express arg as BCD char code. */
  239.           argp[0] += 6 * (tem / 10);
  240.           break;
  241.  
  242.         case 'D':        /* %D means weird Delta Data transformation */
  243.           argp[0] -= 2 * (tem % 16);
  244.           break;
  245.         }
  246.     }
  247.       else
  248.     /* Ordinary character in the argument string.  */
  249.       ordinary:
  250.     *op++ = c;
  251.     }
  252.   *op = 0;
  253.   while (doup-- > 0)
  254.     strcat (op, up);
  255.   while (doleft-- > 0)
  256.     strcat (op, left);
  257.   return outstring;
  258. }
  259.  
  260. #ifdef DEBUG
  261.  
  262. main (argc, argv)
  263.      int argc;
  264.      char **argv;
  265. {
  266.   char buf[50];
  267.   int args[3];
  268.   args[0] = atoi (argv[2]);
  269.   args[1] = atoi (argv[3]);
  270.   args[2] = atoi (argv[4]);
  271.   tparam1 (argv[1], buf, "LEFT", "UP", args);
  272.   printf ("%s\n", buf);
  273.   return 0;
  274. }
  275.  
  276. #endif /* DEBUG */
  277.